package com.kcht.itsed.mws.aleframework.biz;

import com.kcht.itsed.mws.aleframework.intf.IActiveSpecContainer;
import com.kcht.itsed.mws.aleframework.intf.IActiveSpecScheduler;
import com.kcht.itsed.mws.aleframework.intf.IReaderCommandFactory;
import com.kcht.itsed.mws.aleframework.intf.IReaderCommandHelper;
import com.shhic.itsed.mws.deviceintf.ReaderCommand;
import com.shhic.itsed.mws.deviceintf.ReaderCommandType;
import com.kcht.itsed.mws.devicescheduler.facade.IPhysicalReaderLinkManager;
import com.kcht.itsed.mws.devicescheduler.facade.IReaderCommandNotifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Component
public class ActiveSpecScheduler implements IActiveSpecScheduler {

	@Autowired
	IActiveSpecContainer activeSpecContainer;
	@Autowired
	IReaderCommandNotifier readerCommandNotifier;
	@Autowired
	ReaderCommandFactoryGenerator readerCommandFactoryGenerator;
	@Autowired
	IPhysicalReaderLinkManager prLinkManager;	//物理阅读器链路管理器
	
	private Map<String,Set<ReaderCommand>> readerCommandContainer = new HashMap<>();	//key是阅读器ID
	
	@Override
	public synchronized void shuffleSpec() {
		readerCommandContainer.clear();
		activeSpecContainer.getAllSpecs().forEach(spec ->{
			IReaderCommandFactory rcf = readerCommandFactoryGenerator.generateFullyReaderCommands(spec);
			Set<ReaderCommand> allReaderCommandsOfASpec = rcf.generateReaderCommands();
			groupReaderCommandByReader(allReaderCommandsOfASpec);
		});
		activeSpecContainer.notifyAllActiveSpecRefresh();
		
		Set<ReaderCommand> combinedWholeReaderCommands = combineWholeReaderCommand();
		shufflePhysicalReaderLink(combinedWholeReaderCommands);
		readerCommandNotifier.notifyCycleReaderCommand(combinedWholeReaderCommands);
	}

	private void shufflePhysicalReaderLink(Set<ReaderCommand> combinedWholeReaderCommands) {
		// 混合处理物理阅读器链路
		Set<String> readerIpList = combinedWholeReaderCommands.stream().map(r->r.getReaderIp()).collect(Collectors.toSet());
		prLinkManager.refreshAllActiveReader(readerIpList);
	}

	protected void groupReaderCommandByReader(Set<ReaderCommand> fullyReaderCommands) {
		//合并fullyReaderCommands中的各个ReaderCommand，一个物理阅读器一个，填入到readerCommandContainer域中
		fullyReaderCommands.forEach(rc->{
			String readerId = rc.getReaderId();
			if(!readerCommandContainer.containsKey(readerId))readerCommandContainer.put(readerId, new HashSet<ReaderCommand>());
			readerCommandContainer.get(readerId).add(rc);
		});
	}
	
	protected Set<ReaderCommand> combineWholeReaderCommand(){
		//合并combineWholeReaderCommand域中的各个阅读器指令，返回一个物理阅读器一个ReaderCommand的组
		Set<ReaderCommand> cmdSet = new HashSet<>();
		readerCommandContainer.values().forEach(rcSet->cmdSet.add(combineReaderCommand(rcSet)));
		return cmdSet;
	}
	
	private ReaderCommand combineReaderCommand(Set<ReaderCommand> rcSet) {
		//合并阅读器指令
		ReaderCommand p = rcSet.iterator().next();	//任意取一个作为原型
		ReaderCommand hrc = new ReaderCommand();
		hrc.setReaderClass(p.getReaderClass());
		hrc.setReaderCommandType(ReaderCommandType.CYCLE);
		hrc.setReaderId(p.getReaderId());
		hrc.setReaderIp(p.getReaderIp());
		hrc.setReaderPort(p.getReaderPort());
		IReaderCommandHelper rch = ReaderCommandOfSingleReaderHelper.getInstance(rcSet);
		hrc.setReadTagFieldList(rch.generateCombinedTagFieldSet());
		hrc.setAntennaProtocolSet(rch.generateCombinedAntennaProtocolSet());
		return hrc;
	}
}
